

#include "iot_server.h"

//extern pthread_mutex_t coord_mutex;//创建互斥锁
//extern char send_mob_buf[SIZE]; //发送监控数据字符串给移动端的缓冲区

char mob_str[4][20]= {0};										

void dev_control(int dev, int on)
{
	int fd;
	if (dev == 1) {
		fd = open("/dev/led", O_RDWR);
		if(fd < 0) {
			perror("open");
			exit(1);
		}
		if (on == 1)
			printf("led2 green on\n");
		else if (on == 0)
			printf("led2 green off\n");
		write(fd, &on, sizeof(on));
		close(fd);
		
	}
	if (dev == 2) {
		if (on == 1)
			printf("led2 orange on\n");
		else if (on == 0)
			printf("led2 orange off\n");
	}

}

int do_login(char *uname, char *pass)   //根据结构体传的姓名与密码进行查找，判断在表中是否有这个用户信息。
{	
	
	char sql[SIZE] = {0};
	char *errmsg;
	char **result;
	int nrow;
	int ncolumn;
	int ret;
	
	sqlite3 *mydb;
	ret = sqlite3_open("myiot.db", &mydb); //打开数据库文件,如果不存在就创建
	if (ret != SQLITE_OK) {
		fprintf(stderr, "sqlite3_open fail: %s\n", sqlite3_errmsg(mydb));
		exit(1);
	}
	sprintf(sql, "select * from users where uname = \"%s\" and  pass = \"%s\";", uname, pass);
	printf("sql=%s\n", sql);
	if (sqlite3_get_table(mydb, sql, &result, &nrow, &ncolumn, &errmsg) != SQLITE_OK)   //如果失败，将出错信息打印，并将失败提示传给客户端。只需要查找是否存在即可。
	{
		ret = 9;
	}
	else {                                       
		if (nrow == 0)                               //查询到零条也代表查询失败，此用户未注册，返回给客户端                              
			ret = 9;
		else
			ret = 1;                 //查询到符合条件的数据，将查询成功写入结构体，传给客户端。
	}
	sqlite3_free_table(result);                      //释放查询的信息。
	sqlite3_close(mydb);
	return ret;
}


void * recv_mob_thread(void *arg)
{
	int ret;
	char buf[SIZE];
	int newfd = *((int *)arg);
	int dev = -1, on = -1;
	while (1) {
		bzero(buf, SIZE);
		ret = recv(newfd, buf, sizeof(buf), 0); //类似read  阻塞
		if (ret < 0) {
			printf("recv fail\n");
			break;
		}
		//将socket中接收数据缓冲区解析
		bzero(mob_str[0], strlen(mob_str[0])); 
		bzero(mob_str[1], strlen(mob_str[1])); 
		bzero(mob_str[2], strlen(mob_str[2])); 
		bzero(mob_str[3], strlen(mob_str[3])); 
		sscanf(buf, "%s%s%s%s", mob_str[0], mob_str[1],mob_str[2], mob_str[3]);
		//printf("0=%s\n", mob_str[0]);
		//printf("1=%s\n", mob_str[1]);
		//printf("2=%s\n", mob_str[2]);
		//printf("3=%s\n", mob_str[3]);

		
		
		//登录处理
		if ( strncmp(mob_str[1], SER_REQ_LOGIN_HEAD, strlen(SER_REQ_LOGIN_HEAD) )== 0) {
			printf("[%d]>收到手机端登录请求\n", __LINE__);
			printf("mob_str[1]=%s\n", mob_str[1]);
			printf("mob_str[2]=%s\n", mob_str[2]);
			printf("mob_str[3]=%s\n", mob_str[3]);
			
			#if 0
			if ((strncmp("robin", mob_str[2],strlen("robin"))==0) &&
				(strncmp("123", mob_str[3],strlen("123"))==0) ) {
				send(newfd, SER_ACK_YES, strlen(SER_ACK_YES), 0);
			} else {
				send(newfd, SER_ACK_NO, strlen(SER_ACK_NO), 0);
			}
			#endif
			
			ret = do_login(mob_str[2], mob_str[3]);
			printf("ret=%d\n", ret);
			if (ret == 9)
				send(newfd, SER_ACK_NO, strlen(SER_ACK_NO), 0);
			else if (ret == 1)
				send(newfd, SER_ACK_YES, strlen(SER_ACK_YES), 0);
			else 
				send(newfd, SER_ACK_NO, strlen(SER_ACK_NO), 0);


			
		//控制命令请求
		} else if (strncmp(mob_str[0], SER_REQ_CMD_HEAD, strlen(mob_str[0])) == 0) {
			printf("[%d]>收到手机端控制请求\n", __LINE__);
			printf("指令:%s %s\n", mob_str[2], mob_str[3]);
			dev = -1;
			on = -1;
			if (strncmp(mob_str[2], "led2_green", strlen(mob_str[2])) == 0) dev = 1;
			if (strncmp(mob_str[2], "led2_orange", strlen(mob_str[2])) == 0) dev = 2;
			if (strncmp(mob_str[3], "on", strlen(mob_str[3])) == 0) on = 1;
			if (strncmp(mob_str[3], "off", strlen(mob_str[3])) == 0) on = 0;
			dev_control(dev, on); //操作开发板设备
		// 获取参数请求
		} else if (strncmp(mob_str[1], SER_REQ_MONI_HEAD, strlen(mob_str[1])) == 0) {
			printf("[%d]>收到手机端获取参数请求\n", __LINE__);
			//if ((strncmp("robin", mob_str[2], strlen("robin")) == 0) &&
			//	(strncmp("123", mob_str[3], strlen("123")) == 0) ) {
				
				send(newfd, SER_ACK_YES, strlen(SER_ACK_YES), 0);
			//} else {
			//	send(newfd, SER_ACK_NO, strlen(SER_ACK_NO), 0);
			//}

		} else if(strncmp(mob_str[1], SER_REQ_MONI_INFO, strlen(mob_str[1])) == 0) {
			//读全局缓冲区数据
			pthread_mutex_lock(&coord_mutex);//上锁
			send(newfd, send_mob_buf, strlen(send_mob_buf), 0);	
			pthread_mutex_unlock(&coord_mutex);//释放锁
			//printf("[%d]>发送给手机端的参数: %s\n", __LINE__, send_mob_buf);
		} else if(strncmp(mob_str[1], SER_REQ_MONI_END, strlen(mob_str[1])) == 0) {
			printf("[%d]>收到手机端获取参数结束请求\n", __LINE__);
		}
		
		
		if (ret == 0) {
			printf("client fd=%d offline\n", newfd);
			break;
		}

	}
	return NULL;

}

